<?php
require_once("Includes/connection.php");
require_once("Includes/functions.php");
session_start();
if(isset($_SESSION['username'])){
	if(isset($_GET['short_name'])){
		$entry_date=array();
		$entry_from=array();
		$entry_answer=array();
		get_poll_data($_GET['short_name'], $entry_date, $entry_from, $entry_answer);
		$query = sprintf("SELECT * FROM polls WHERE short_name='%s'",$_GET['short_name']);
		$result = mysql_query($query);
		$info = mysql_fetch_assoc($result);
		$end = $info['time_end'];
		$start = $info['time_start'];
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
<title>Q&A </title>
</head>
<body>
	<table id="header">
    <tr><td></td>
    <td colspan="2" style="width:1000px; text-align:center;">
    	<table><tr><td width="180">
    	<img src="images/logo_qanda.gif" height="80"/>
		</td><td>
        <h1 style="height:auto;">ACM RUM Q&A </h1>
		</td></tr></table>
    </td>
    <td></td></tr>
    <tr><td style="border-bottom:1px solid black;"></td><td style="width:180px;"></td><td style="border-bottom:1px solid black; width:813px;"></td><td style="border-bottom:1px solid black;"></td></tr>
    </table>
    <table id="container">
        <tr>
        <td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-top:10px; padding-left:5px; background-color:#FC0;">
        <a href="home.php">Home</a>
        </td><td rowspan="6" style="border-right:1px solid #CCC; border-bottom:1px solid #CCC;  padding-top:15px;">
            <table id="main_content">
            <tr>
            <td>
				<?php
				if (mysql_num_rows($result) > 0){
					echo '<table id="main_content_view">
					<tr>
					<th>ID</th>
					<th>Type</th>
					<th>Time Start</th>
					<th>Time End</th>
					<th>Name</th>
					<th>Short Name</th>
					<th>Registration Required</th>
					<th>Multiple Entries</th>
					<th>SMS Entries</th>
					<th>Web Entries</th>
					<th>Tag Cloud</th>
					</tr><tr>
					<td><a href="poll_data.php?short_name='.$info['short_name'].'">'.$info['id'].'</a></td>
					<td>'.$info['type'].'</td>
					<td>'.$info['time_start'].'</td>
					<td>'.$info['time_end'].'</td>
					<td><a href="poll_data.php?short_name='.$info['short_name'].'">'.$info['name'].'</a></td>
					<td>'.$info['short_name'].'</td>
					<td>'.$info['registration_required'].'</td>
					<td>'.$info['multiple_entries'].'</td>
					<td>'.$info['sms_entries'].'</td>
					<td>'.$info['web_entries'].'</td>
					<form action="tag_cloud.php" method="get">
					<input type="hidden" name="short_name" value="'.$info['short_name'].'">
					<td><input type="submit" name="tag" value="View"/></td>
					</form>
					</tr></table>';
				}
				else{
					echo '<h2>This poll was not found in our database.</h2>';
				}
                ?>
            </td></tr>
            <tr><td>
                <table><?php
				$x=1;
				if($info['multiple_entries']=='No'){
					delete_multiple_entries($entry_date, $entry_from, $entry_answer);
				}
				if($info['sms_entries']=='No'){
					delete_sms_entries($entry_date, $entry_from, $entry_answer);
				}
				if($info['web_entries']=='No'){
					delete_web_entries($entry_date, $entry_from, $entry_answer);
				}
                for($i=0; $i<count($entry_date); $i++){					
					if($end >= $entry_date[$i] && $start <= $entry_date[$i]){
						echo '<tr><td><b>#'.$x.'</b>';
						echo '</td></tr><tr><td><b>From: </b>'.$entry_from[$i];
						echo '</td></tr><tr><td><b>Date: </b>'.$entry_date[$i];
						echo '</td></tr><tr><td style="border-bottom:1px solid #CCC;"><b>Answer: </b>'.$entry_answer[$i];
						echo '</td></tr><tr><td style="padding-bottom:10px;"></td></tr>';
						$x++;
					}
					else{
						echo '<tr><td style="color:red;"><b>#'.$x.'</b>';
						echo '</td></tr><tr><td style="color:red;"><b>From: </b>'.$entry_from[$i];
						echo '</td></tr><tr><td style="color:red;"><b>Date: </b>'.$entry_date[$i];
						echo '</td></tr><tr><td style="border-bottom:1px solid #CCC; color:red;"><b>Answer: </b>'.$entry_answer[$i];
						echo '</td></tr><tr><td style="padding-bottom:10px;"></td></tr>';
						$x++;
					}
                }
				if($x==1){
					echo '<tr><td><p>No answers have been submited<p></td></tr>';
				}
                ?></table>
            </td>
			</tr>
            </table>
        </td></tr>
        <tr><td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-left:5px; background-color:#FC0;">
        <a href="create_poll.php">Create Poll</a>
        </td></tr>
        <tr><td style="border:1px solid black; border-right:none; background-color:#FFF; width:176px; height:30px; padding-left:5px;">
        <a href="view_polls.php">View Polls</a>
        </td></tr>
        <tr><td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-left:5px; background-color:#FC0;">
        <a href="manage_accounts.php">Manage Accounts</a>
        </td></tr>
        <tr><td style="border-left:1px solid black; border-right:1px solid black; width:176px; height:30px; padding-left:5px; background-color:#FC0;">
        <a href="index.php">Logout</a>
        </td></tr>
        <tr><td style="border:1px solid black; border-top:none; width:176px; padding-left:5px; background-color:#FC0;">
        </td>
        </tr>
    </table>
</body>
</html>
<?php 
}
else{
	header('Location: index.php');
}
?>